package myJs.api

import myJs.myPkg.MyFetch
import myJs.tool.Tool
import org.scalajs.dom.{FormData, XMLHttpRequest}
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo
import shared.pojo.Pojo.ValidData
import shared.pojo.Sample.IdStrsData
import slinky.web.html.s
import upickle.default._

/**
 * Created by yz on 19/5/2022
 */
object SampleIdApi {

  import Pojo.sampleId._

  val baseUrl = s"${Api.apiUrl}/panda/sampleId"

  def getNewIds(request: GetNewIds.Request) = {
    val url = s"${baseUrl}/getNewIds"
    MyFetch.postRnText(url = s"${url}", data = write(request),
      headers = Map("Content-Type" -> "application/json", "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[List[String]](str)
    }
  }

  def add2Db(request: Add2Db.Request) = {
    val url = s"${Api.apiUrl}/panda/sampleId/add2Db"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def all = {
    val url = s"${baseUrl}/all"
    MyFetch.getRnStr(url = s"${url}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      read[List[ujson.Obj]](str)
    }
  }

  def sampleIdCheck(request: SampleIdCheck.Request) = {
    val url = s"${baseUrl}/sampleIdCheck"
    MyFetch.postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json")).map { str =>
      read[ValidData](str)
    }
  }

  def delete(idV: String) = {
    val url = s"${baseUrl}/delete"
    MyFetch.deleteRnStr(url = s"${url}?id=${idV}", headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }

  def deletes(request: IdStrsData) = {
    val url = s"${baseUrl}/deletes"
    MyFetch.deleteRnStr(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json",
      "X-Authorization" -> s"Bearer ${Tool.getToken}")).map { str =>
      str
    }
  }


}
